//
//  XFYLabelList.m
//  SDKSample
//
//  Created by 小肥羊 on 15/7/3.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYLabelList.h"

#pragma mark - 自适应Label列表
@implementation XFYLabelList
#pragma mark - 初始化
- (id)initWithFrame:(CGRect)frame withArray:(NSMutableArray *)textArray
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        self.textArray = textArray;
    }
    return self;
}

#pragma mark - Set
- (void)setTextArray:(NSMutableArray *)textArray
{
    _textArray = textArray;
    for(UIView *view in [self subviews])
    {
        [view removeFromSuperview];
    }
    float lebelWidth = (self.frame.size.width - 12 * 3)/2;
    for (NSInteger i =0 ; i < _textArray.count; i++) {
        XFYAdaptiveLabel *label = [[XFYAdaptiveLabel alloc] init];
        label.tag = LabelTag(i);
        label.labelMaxWidth = lebelWidth;
        label.delegate = self;
        [label setText:_textArray[i]];
        [self addSubview:label];
    }
}

#pragma mark - 显示
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (!self.textArray || self.textArray.count == 0) {
        [self setFrame:(CGRect){self.frame.origin, CGSizeMake(self.frame.size.width, 0)}];
        return;
    }
    int rowCount = (int)((self.textArray.count - 1)/2 + 1);
    float rowHeight = 0.0;
    for (NSInteger row = 0; row < rowCount; row++) {
        float labelRowTotalLength = 0;
        NSInteger maxColumn = 0;
        for (NSInteger Column = 0; Column < 2; Column++) {
            NSInteger tag = row * 2 + Column;
            if (tag < self.textArray.count) {
                XFYAdaptiveLabel *xfyLabel = (XFYAdaptiveLabel *)[self viewWithTag:LabelTag(tag)];
                labelRowTotalLength = labelRowTotalLength + xfyLabel.frame.size.width;
                maxColumn = Column;
            }
        }
        float lastStartX = 0;
        for (NSInteger Column = 0; Column < 2; Column++) {
            float spacing = (self.frame.size.width - labelRowTotalLength)/(maxColumn + 2);
            NSInteger tag = row * 2 + Column;
            if (tag < self.textArray.count) {
                XFYAdaptiveLabel *xfyLabel = (XFYAdaptiveLabel *)[self viewWithTag:LabelTag(tag)];
                rowHeight = xfyLabel.frame.size.height;
                [xfyLabel setFrame:(CGRect){CGPointMake(lastStartX + spacing, 8 + (rowHeight + 8) * row), xfyLabel.frame.size}];
                lastStartX = spacing + xfyLabel.frame.size.width;
            }
        }
    }
    [self setFrame:(CGRect){self.frame.origin, CGSizeMake(self.frame.size.width, (rowHeight + 8) * rowCount + 8)}];
}

#pragma mark - 删除按钮代理
- (void)operation:(XFYAdaptiveLabel *)label
{
    NSLog(@"点击了%@的操作",label.textLabel.text);
    [self.textArray removeObjectAtIndex:ArrayIndex(label.tag)];
    self.textArray = self.textArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end



#pragma mark - 固定Label列表
@implementation XFYFixedLabelList
#pragma mark - 初始化
- (id)initWithFrame:(CGRect)frame withArray:(NSMutableArray *)textArray
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        self.textArray = textArray;
    }
    return self;
}
#pragma mark - Set
- (void)setTextArray:(NSMutableArray *)textArray
{
    _textArray = textArray;
    for(UIView *view in [self subviews])
    {
        [view removeFromSuperview];
    }
    float lebelWidth = (self.frame.size.width - 12 * 3)/2;
    for (NSInteger i =0 ; i < _textArray.count; i++) {
        XFYFixedLabel *label = [[XFYFixedLabel alloc] initWithFrame:CGRectMake(0, 0, lebelWidth, 26)];
        label.tag = LabelTag(i);
        label.delegate = self;
        [label.textLabel setText:_textArray[i]];
        [self addSubview:label];
    }
}

#pragma mark - 显示
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (!self.textArray || self.textArray.count == 0) {
        [self setFrame:(CGRect){self.frame.origin, CGSizeMake(self.frame.size.width, 0)}];
        return;
    }
    int rowCount = (int)((self.textArray.count - 1)/2 + 1);
    float rowHeight = 0.0;
    for (NSInteger row = 0; row < rowCount; row++) {
        float lastStartX = 12;
        for (NSInteger Column = 0; Column < 2; Column++) {
            NSInteger tag = row * 2 + Column;
            if (tag < self.textArray.count) {
                XFYFixedLabel *xfyLabel = (XFYFixedLabel *)[self viewWithTag:LabelTag(tag)];
                rowHeight = xfyLabel.frame.size.height;
                [xfyLabel setFrame:(CGRect){CGPointMake(lastStartX, 8 + (rowHeight + 8) * row), xfyLabel.frame.size}];
                lastStartX = 24 + xfyLabel.frame.size.width;
            }
        }
    }
    [self setFrame:(CGRect){self.frame.origin, CGSizeMake(self.frame.size.width, (rowHeight + 8) * rowCount + 8)}];
}

#pragma mark - 删除按钮代理
- (void)fixedOperation:(XFYFixedLabel *)label
{
    NSLog(@"点击了%@的操作",label.textLabel.text);
    [self.textArray removeObjectAtIndex:ArrayIndex(label.tag)];
    self.textArray = self.textArray;
}

@end
